package commands;

import java.net.InetAddress;

import main.InputHandler;
import util.*;

public class ListCommand extends Command {
	public static String name = "list";
	public static String help = "Name: list \n>list {str:opt('vars','commands','network','info')} \n>list (group to list) \n>Lists either allocated variables, all commands, all connected clients, or network info.";
	
	public static boolean test(InputHandler handler, StringList words){
		if(StringChecker.testWord(words, 0, "list")){
			String[] options = {"vars", "commands", "network", "info"};
			int choice = StringChecker.testWord(words, 1, options);
			switch(choice){
			case 0:
				Object[] vals = DefineCommand.getValues().toArray();
				Object[] names = DefineCommand.getKeys().toArray();
				for(int i = 0; i < vals.length; i++){
					handler.terminal.write(names[i]+":  "+vals[i]);
				}
				break;
			case 1:
				for(Class<? extends Command> c: Command.commands){
					try {
						handler.terminal.write(c.getField("name").get(null).toString());
					} catch (Exception e){
						e.printStackTrace();
						handler.terminal.write("Error finding name encountered");
					}
				}
				break;
			case 2:
				Object[] socketNames = OpenPortCommand.getKeys().toArray();
				Object[] ips = OpenPortCommand.getSockets().toArray();
				for(int i = 0; i < ips.length; i++){
					handler.terminal.write(socketNames[i] + ": "+((ConnectionData)ips[i]).socket.getInetAddress().getHostAddress());
				}
				break;
			case 3:
				try{
					handler.terminal.write(InetAddress.getLocalHost().getHostAddress());
				}catch(Exception e){}
				return true;
			}
			return true;
		}
		return false;
	}

}
